//
//  UIBarButtonItem+Extension.swift
//  myBlog
//
//  Created by mac2014 on 16/3/25.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit

// MARK: - 扩展里面只能添加便利构造函数，便利构造函数一定要调用指定构造函数来创建对象。
extension UIBarButtonItem {
    convenience init(imageName: String, target: AnyObject, action: Selector) {
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        self.init(customView: button)
    }
    
    
    //创建一个带按钮的UIBarButtonItem
    class func createBarButton(imageName: String, target: AnyObject, action: Selector) -> UIBarButtonItem {
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        return UIBarButtonItem(customView: button)
    }
}
